package com.jiyinit.web.core.mobile.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.dto.DailyLogRequest;
import com.jiyinit.web.core.business.dto.MessageRequest;
import com.jiyinit.web.core.business.entity.TabCoreTask;
import com.jiyinit.web.core.business.entity.TabCoreTaskMessage;
import com.jiyinit.web.core.business.service.DailyLogServiceImpl;
import com.jiyinit.web.core.business.service.TaskMessageServiceImpl;
import com.jiyinit.web.core.business.vo.TaskMessageVo;
import com.jiyinit.web.core.common.logic.ParamCheckLogic;
import com.jiyinit.web.core.common.logic.ValidateResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by lr on 2017/12/28.
 */
@Controller
@RequestMapping("/mobile/message")
public class MessageController {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageController.class);

    @Resource(name = "core.paramCheckLogic")
    private ParamCheckLogic paramCheckLogic;

    @Resource(name = "core.taskMessageServiceImpl")
    private TaskMessageServiceImpl taskMessageService;
    /**
     * 分页查询说说
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "findByPage")
    public ResponseEntity<ResponseDto> findByPage(HttpServletRequest request){
        MessageRequest messageRequest = getJsonToTaskRequest(request);

        PagingResult<TaskMessageVo> result = taskMessageService.findByPage(messageRequest);

        return new ResponseEntity<>( ResponseDto.responseOK(result), HttpStatus.OK);
    }

    /**
     * 添加说说
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "add")
    public ResponseEntity<ResponseDto> addMessage(HttpServletRequest request){
        MessageRequest messageRequest = getJsonToTaskRequest(request);

        ValidateResult<MessageRequest> validateResult = paramCheckLogic.checkParamValidate(messageRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }

        int num = taskMessageService.addDailyLogMessage(messageRequest);

        if(num > 0)
            return new ResponseEntity<>( ResponseDto.responseOK(""), HttpStatus.OK);
        return new ResponseEntity<>( ResponseDto.responseFail("新建失败！"), HttpStatus.OK);
    }

    private MessageRequest getJsonToTaskRequest(HttpServletRequest request){
        String content = (String) request.getAttribute("request_json");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        MessageRequest messageRequest = (MessageRequest) JsonEntityTransform.json2Object(logJson, MessageRequest.class);
        return messageRequest;
    }
}
